package com.shenjialin.util;

import com.shenjialin.Game.Bullet;
import com.shenjialin.Game.Explode;

import java.util.ArrayList;
import java.util.List;

public class ExplodesPool {
    public static final int DEFAULT_SIZE = 100;
    public static final int POOL_MAXSIZE = 150;
    // 用于保存所有爆炸效果的容器
    private static List<Explode> pool = new ArrayList<>();
    // 创建size个对象

    static {
        for (int i = 0; i < DEFAULT_SIZE; ++i) {
            pool.add(new Explode());
        }
    }

    /**
     * 从池塘中获得爆炸 对象
     * @return
     */
    public static Explode get() {
        Explode explode = null;
        if (pool.size() == 0) {// 对象池中没有对象可以使用
            explode = new Explode();
        }else{// 从对象池中获得一个子弹对象
            explode = pool.remove(0);
        }
        return explode;
    }

    /**
     * 子弹对象的回收 归还到对象池中
     */
    public static void theReturn(Explode explode) {
        if(pool.size() >= POOL_MAXSIZE){
            return;
        }
        pool.add(explode);
    }
}
